package com.colorful.insuranceproject.home;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.SharePageActivity;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.ConsultBean;
import com.colorful.insuranceproject.bean.HealthInfoBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.HomeRequest;
import com.colorful.insuranceproject.utils.ParseHtmlCode;
import com.lovely3x.common.utils.Response;
import com.lovely3x.library.benas.ShareContent;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by handsome-Bob on 2017/10/27.
 * <p>
 * 咨询详情
 */
public class ConsultDetailsActivity extends TitleActivity implements View.OnClickListener {

    private static final int GET_CONSULT_DETAILS = 0X1;

    public static final String EXTRA_DETAILS = "extra_details";

    HomeRequest homeRequest;

    @Bind(R.id.tv_title)
    TextView title;

    @Bind(R.id.tv_from)
    TextView from;

    @Bind(R.id.webView)
    WebView webView;

    private HealthInfoBean healthInfoBean;

    @Override
    protected int getContentView() {
        return R.layout.activity_consult_detials;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.consult_details));
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.drawable.icon_zhuanzai);
        imageView.setId(R.id.img_share);
        addRightView(imageView);
        imageView.setVisibility(View.GONE);
        imageView.setOnClickListener(this);

        title.setText(healthInfoBean.getTitle());
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        healthInfoBean = bundle.getParcelable(EXTRA_DETAILS);
    }

    @Override
    protected void onViewInitialized() {
        homeRequest = new HomeRequest(getHandler());
        WebSettings webSettings = webView.getSettings();
        webSettings.setSupportZoom(true);

        showProgressCircle("加载中.....");

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissProgressCircle();
            }
        });

        /*
         *   获取详情
         */
        homeRequest.getConsultDetails(healthInfoBean.getId(), GET_CONSULT_DETAILS);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_CONSULT_DETAILS:
                if (response.isSuccessful) {
                    setPageData((ConsultBean) response.obj);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    /**
     * set page data
     *
     * @param bean data
     */
    private void setPageData(ConsultBean bean) {
        if (from != null) {
            from.setText(getString(R.string.from_to, healthInfoBean.getAuthor(), bean.getCreateTime()));
            webView.loadDataWithBaseURL(null, ParseHtmlCode.parseHtmlCode(bean.getContent()), "text/html", "utf-8", null);
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    /**
     * 分享按钮
     */
    @Override
    public void onClick(View v) {
        String url;
//                if (shareBean.getConsultUrl() != null && !shareBean.getConsultUrl().equals("")) {
        url = "http://www.baidu.com";
//                } else {
//                    url = URLConst.OFFICIAL_WEBSITE_ADDRESS;
//                }
        String name = getString(R.string.app_name);
        String desc = "可康安" /*shareBean.getArticleTitle()**/;
        ShareContent shareContent = new ShareContent(name, url, desc, null);
        shareContent.imageResource = R.mipmap.logo;
        SharePageActivity.launchMe(this, shareContent);
    }
}
